<template>
  <div class="container">
    <div class="table">
      <el-table :data="tableData" style="width: 100%; height: 100%" v-bind="$attrs" :header-cell-style="{ height: '25px', fontSize: '15px', backgroundColor: '#f2f2f2' }">
        <template v-for="(column, index) in columns" :key="column.id">
          <el-table-column v-if="column.prop === 'index'" type="index" align="center" :label="column.label" width="100"></el-table-column>
          <template v-else>
            <el-table-column v-if="!column.hide" :sortable="column.sort" align="center" show-overflow-tooltip :prop="column.prop" :label="column.label" :width="column.width">
              <template #default="scope">
                <slot :name="column.prop" :row="scope.row" :index="scope.$index" :column="scope.column">
                  {{ scope.row[column.prop] }}
                </slot>
              </template>
            </el-table-column>
          </template>
        </template>
      </el-table>
    </div>
    <div class="pagination">
      <el-pagination v-model:current-page="currentPage" v-model:page-size="pageSize" :page-sizes="[10, 20, 30, 40, 50]" layout="prev, pager, next,sizes,total" :total="props.page.total" @size-change="handleSizeChange" @current-change="handleCurrentChange" />
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const props = defineProps({
  tableData: {
    type: Array,
    default: [],
  },
  columns: {
    type: Array,
    default: [],
  },
  page: {
    type: Object,
    default: () => {
      return {
        currentPage: 1,
        pageSize: 10,
        total: 10,
      }
    },
  },
})

const currentPage = ref(props.page.currentPage)
const pageSize = ref(props.page.pageSize)
const emits = defineEmits(['handleSizeChange', 'handleCurrentChange'])
// 改变页面显示的数据
const handleSizeChange = (value) => {
  emits('handleSizeChange', value)
}
const handleCurrentChange = (value) => {
  emits('handleCurrentChange', value)
}
</script>

<style scoped lang="scss">
.table {
  width: calc(100% - 5px);
  height: calc(100vh - 350px);
}
.pagination {
  width: 100%;
  margin: 10px auto;
  display: flex;
  justify-content: center;
}
</style>
